<template>
  <div>
<!-- 评论组件 -->
<van-list
  v-model="loading"
  :finished="finished"
  finished-text="没有更多了"
  @load="onLoad"
>
  <!-- <van-cell v-for="item in list" :key="item.com_id+''" :title="item.content" /> -->
<commentitem @click-reply="$emit('click-reply',$event)" v-for="item in list" :key="item.com_id+''" :commentitem="item"/>
</van-list>
  </div>
</template>

<script>
import { getcommitlist } from '@/api/article.js'
import commentitem from '@/views/Detail/components/commentitem'

export default {
  data () {
    return {
      // list: [],//已父传子过来 所以写在下面props :list
      loading: false,
      finished: false,
      offset: null,
      limit: 10
    }
  },
  props: {
    type: {
      type: String,
      default: 'a'
    },
    source: {
      type: [Number, Object, String],
      require: true
    },
    list: {
      type: Array,
      default: () => []
    }
  },
  created () {

  },
  components: {
    commentitem
  },
  methods: {
    async onLoad () {
      try {
        const { results, last_id: lastId, total_count: total } = await getcommitlist({
          type: this.type,
          source: this.source,
          offset: this.offset,
          limit: this.limit
        })
        this.$emit('on-success', total)
        this.list.push(...results)
        this.loading = false
        if (!results.length) {
          this.finished = true
        } else {
          this.offset = lastId
        }
        // console.log(res)
      } catch (error) {
        console.log(error)
      }
    }
    // onLoad () {
    //   // 异步更新数据
    //   // setTimeout 仅做示例，真实场景中一般为 ajax 请求
    //   setTimeout(() => {
    //     for (let i = 0; i < 10; i++) {
    //       this.list.push(this.list.length + 1)
    //     }

    //     // 加载状态结束
    //     this.loading = false

    //     // 数据全部加载完成
    //     if (this.list.length >= 40) {
    //       this.finished = true
    //     }
    //   }, 1000)
    // }
  }
}
</script>

<style scoped lang='less'>

</style>
